package com.welsy.authoring.vo.course
{
	import com.welsy.authoring.business.TestQNames;
	import com.welsy.authoring.vo.course.pattern.*;
	import com.welsy.util.XMLUtils;
	
	[Bindable]	
	public class Question extends CourseItem
	{
		public var id:String = "0";		
		public var title:String = "";		
		public var position:String = "0";		
		public var weight:String = "";	
	    public var fileRepositoryID:String = "0";	    
	    public var fileName:String = "";
	    public var trackStatus:String = "0";
	    
		// The array of child elements
      	private var _items:Array;

      	public function Question() {
        	_items = new Array();
      	}
      	
      	public function setProperties(list:XMLList):void {      	
	     	this.id = list.child (TestQNames.qnQuestionID);
    		this.title = list.child (TestQNames.qnTitle);
    		this.position = list.child (TestQNames.qnPosition);
    		this.weight = list.child (TestQNames.qnWeight);
    		this.fileRepositoryID = list.child (TestQNames.qnElElementFileID);
    		this.fileName = list.child (TestQNames.qnElElementFileName);
    		this.trackStatus = list.child (TestQNames.qnElTrackStatus);        
      	}
      	
      	override public function addItem(item:ICourseItem):void {
	        _items.push(item);
	    }
	
	    override public function removeItem(item:ICourseItem):void {
	        var i:uint;
	        // Loop through all the child elements.
	        for(i = 0; i < _items.length; i++) {
	           // If one of the elements matches the 
	           // parameter remove it from the array.
	           if(_items[i] == item) {
	              _items.splice(i, 1);
	              break;
	           }
	        }
	    }
	    
	    public function getCollection():Array {         
        	return _items;
      	}
      
	    override public function iterator():ICourseIterator {	         
	    	return new ArrayCourseIterator(_items);
	    }
	    
	    public function getXML ():XML {
	    	var utilsXml:XMLUtils = new XMLUtils;	    	
	    	var title:String = this.title 
	    	title = utilsXml.Convert(title)	    	
	    	var sItem:String = "<question label='" + title + "'";
	    	sItem += " id='" + this.id + "' position='" + this.position + "' weight='" + this.weight + "'";
	    	sItem += " fileRepositoryID='" + this.fileRepositoryID + "' fileName='" + this.fileName + "'/>";
			return new XML (sItem).normalize();
	   }	    
	}
}